package xhe.lib.net;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;

@Component
public class Http {

	@Autowired
	private RestTemplateBuilder restTemplateBuilder;

	/**
	 * All rights Reserved, Designed By fane
	 *
	 * @param url      地址
	 * @param paramMap 参数
	 * @return String
	 * @title: HttpUtil.java
	 * @package: com.perfect
	 * @description: http服务post请求
	 * @author: fane
	 * @email: fane.wang@foxmail.com
	 * @date: 2019年7月4日 上午9:51:46
	 * @copyright: 2019 fane All rights reserved.
	 * @version: V1.0
	 */
	public String post(String url, MultiValueMap<String, Object> paramMap) {
		return restTemplateBuilder.build().postForObject(url, paramMap, String.class);
	}

	// HTTP服务Post请求
	public String post(String url, String parameter) {
		return restTemplateBuilder.build().postForObject(url, parameter, String.class);
	}

	// HTTP服务Post请求
	public String postSoap(String url, String parameter) {
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "text/xml; charset=UTF-8");
		HttpEntity<String> formEntity = new HttpEntity<String>(parameter, headers);
		return restTemplateBuilder.build().postForObject(url, formEntity, String.class);
	}

	// HTTP服务Post请求
	public String postJson(String url, String parameter) {
		HttpHeaders headers = new HttpHeaders();
		MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
		headers.setContentType(type);
		headers.add("Accept", MediaType.APPLICATION_JSON.toString());
		HttpEntity<String> formEntity = new HttpEntity<String>(parameter, headers);
		return restTemplateBuilder.build().postForObject(url, formEntity, String.class);
	}

	/**
	 * All rights Reserved, Designed By fane
	 *
	 * @param url      地址
	 * @param paramMap 参数
	 * @return String
	 * @title: HttpUtil.java
	 * @package: com.perfect
	 * @description: http服务get请求
	 * @author: fane
	 * @email: fane.wang@foxmail.com
	 * @date: 2019年7月4日 上午9:53:21
	 * @copyright: 2019 fane All rights reserved.
	 * @version: V1.0
	 */
	public String get(String url, Map<String, Object> paramMap) {
		// 封装参数，这里是HashMap Map<String, Object> paramMap = new HashMap<String, Object>();
		// url需要使用占位符 http://localhost:8080/aa?a={a}
		// 使用getForObject请求接口
		return restTemplateBuilder.build().getForObject(url, String.class, paramMap);
	}

	public void get1(String url, Map<String, Object> paramMap) {
		// 封装参数，这里是HashMap Map<String, Object> paramMap = new HashMap<String, Object>();
		// url需要使用占位符 http://localhost:8080/aa?a={a}
		// 1、使用getForObject请求接口
		String result1 = restTemplateBuilder.build().getForObject(url, String.class, paramMap);
		System.out.println("result1====================" + result1);

		// 2、使用exchange请求接口
		HttpHeaders headers = new HttpHeaders();
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,
				headers);
		ResponseEntity<String> response2 = restTemplateBuilder.build().exchange(url, HttpMethod.GET, httpEntity,
				String.class, paramMap);
		System.out.println("result2====================" + response2.getBody());
	}

	public void post1(String url, MultiValueMap<String, Object> paramMap) {
		// MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String,
		// Object>();
		// 封装参数，千万不要替换为Map与HashMap，否则参数无法传递
		// 1、使用postForObject请求接口
		String result = restTemplateBuilder.build().postForObject(url, paramMap, String.class);
		System.out.println("result1==================" + result);

		// 2、使用postForEntity请求接口
		HttpHeaders headers = new HttpHeaders();
		HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(paramMap,
				headers);
		ResponseEntity<String> response2 = restTemplateBuilder.build().postForEntity(url, httpEntity, String.class);
		System.out.println("result2====================" + response2.getBody());

		// 3、使用exchange请求接口
		ResponseEntity<String> response3 = restTemplateBuilder.build().exchange(url, HttpMethod.POST, httpEntity,
				String.class);
		System.out.println("result3====================" + response3.getBody());
	}

}
